package com.zosean.sky.stringItem;

import javax.microedition.lcdui.Font;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author sky
 * @version 1.0
 */
public class StringProperties {
    // Source string
    private String sourceString = null;
    // font the face, style and size of the source string
    private int font_Face = Font.FACE_SYSTEM;
    private int font_Style = Font.STYLE_PLAIN;
    private int font_Size = Font.SIZE_SMALL;
    // The font face of the source string
    private Font defaultFont = Font.getFont(font_Face, font_Style, font_Size);
    // The color of the source string,default the white
    private int font_Color = 0xffffff;
    // The string length
    private int sourceStringLength = 0;
    /**
     * Default Constructor Method
     */
    public StringProperties(String stringValue) {
        if (stringValue == null)
            return;
        sourceString = stringValue;
    }

    /**
     * Structure with a parameter
     */
    public StringProperties(String stringValue, Font fontValue, int colorValue) {
        if (stringValue == null)
            return;
        sourceString = stringValue;
        if (fontValue != null)
            defaultFont = fontValue;
        if (colorValue < 0) {
            colorValue = Math.abs(colorValue);
            font_Color = colorValue;
            System.out.println(">> WARNING = " + "The color range is wrong");
        } else
            font_Color = colorValue;
    }

    /**
     * Get the source string
     */
    public String getSourceString() {
        if (sourceString != null)
            return sourceString;
        else {
            System.out.println(">> WARNING = " +
                               "Get the sourceString is  NULL");
            return null;
        }
    }

    /**
     * Set the source string
     */
    public void setSourceString(String sourceStringValue) {
        if (sourceStringValue == null)
            return;
        if (sourceString != null) {
            System.out.println(">> WARNING = " +
                               "Resource string is not empty,has been updated successfully");
        }
        sourceString = sourceStringValue;
    }

    /**
     * Get the string font
     */
    public Font getDefaultFont() {
        if (defaultFont != null)
            return defaultFont;
        else {
            System.out.println(">> WARNING = " + "The source font is NULL");
            return null;
        }
    }

    /**
     * Set the source font
     */
    public void setDefaultFont(Font fontValue) {
        if (fontValue != null)
            defaultFont = fontValue;
    }

    /**
     * Get the source color
     */
    public int getFont_Color() {
        if (font_Color >= 0)
            return font_Color;
        else {
            System.out.println(">>WARNING = " + "The source color is NULL");
            return font_Color;
        }
    }

    /**
     * Set the source color
     */
    public void setFont_Color(int colorValue) {
        if (colorValue <= 0) {
            System.out.println(">> WARNING = " + "The color range is wrong");
            colorValue = Math.abs(colorValue);
        }
        font_Color = colorValue;
    }

    /**
     * Get the source length
     */
    public int getSourceStringLength() {
        if (sourceString != null)
            return sourceString.length();
        else {
            System.out.println(">>WARNING = " +
                               "Length of the string out of range");
            return -1;
        }
    }
    /**
     * According to the index into the string
     */
    public void insertString(int index, String insert_str)
    {
        if(index < 0 || index > sourceString.length())
        {
            System.out.println(">> WARNING = " + "Insert index out of rang of the source string");
            return;
        }
        if(insert_str == null)
        {
            System.out.println(">> WARNING = " + "Insert the string is empty");
            return;
        }
        String tempOne = "";
        String tempTwo = "";
        StringBuffer tempSb = new StringBuffer();
        if(sourceString != null)
        {
            tempOne = sourceString.substring(0, index);
            tempTwo = sourceString.substring(index, sourceString.length());
            tempSb.append(tempOne);
            tempSb.append(insert_str);
            tempSb.append(tempTwo);
        }
        sourceString = tempSb.toString();
//        tempSb = null;
//        tempOne = null;
//        tempTwo = null;
    }
    /**
     * Added by default , insert the end of the source string
     * @param insert_str The insert string
     */
    public void insertString(String insert_str) {
        if (insert_str == null) {
            System.out.println(">> WARNING = " + "Insert the string is empty");
            return;
        }
        if(sourceString != null)
        {
            StringBuffer sb = new StringBuffer();
            sb.append(sourceString);
            sb.append(insert_str);
            sourceString = sb.toString();
            sb = null;
        }
    }
    /**
     * Replace the specified string
     * @param subString String  Is replaced by the word string
     * @param changeString String  replace string
     */
    public void replacementString(String subString, String changeString)
    {
        if(changeString == null)
        {
            System.out.println(">>WARNING = " + "The chagne string is empty");
            return;
        }
        if(subString.equals(""))
        {
            System.out.println(">>WARNING = " + "The sub-string can't empty");
            return;
        }
        if(sourceString == null)
        {
            System.out.println(">> WARNING = " + "The source string is empty");
            return;
        }
        int tempIndex = 0;
        StringBuffer  tempSb = new StringBuffer();
        tempIndex = sourceString.indexOf(subString);
        if(tempIndex != -1)
        {
            tempSb.append(sourceString.substring(0, tempIndex));
            tempSb.append(changeString);
            tempSb.append(sourceString.substring(tempIndex + subString.length(), sourceString.length()));
            sourceString= tempSb.toString();
            System.out.println("SUCCESS : " + "Replace String successful");
        }else
            System.out.println(">>WARNING = " + "Replace the string fails, is no substring");
        if(tempSb != null)
            tempSb = null;
    }
    /**
     * Remove the specified string
     * @param removeStringValue String  Delete the sub-string
     */
    public void removeString(String removeStringValue)
    {
        if(removeStringValue == null || removeStringValue.equals(""))
        {
            System.out.println(">>WARNING = " + "Remove the string is empty or empty string");
            return;
        }
        if(sourceString == null || sourceString.equals(""))
        {
            System.out.println(">>WARNING = " + "The source string is empty or empty string");
            return;
        }
        int tempIndex = 0;
        StringBuffer tempStringBuffer = new StringBuffer();
        tempIndex = sourceString.indexOf(removeStringValue);
        if(tempIndex != -1)
        {
            tempStringBuffer.append(sourceString.substring(0, tempIndex));
            tempStringBuffer.append(sourceString.substring(tempIndex + removeStringValue.length(), sourceString.length()));
            sourceString = tempStringBuffer.toString();
            System.out.println("SUCCESS : " + "Remove the sub-string is successful");
        }else
        {
            System.out.println(">>WARNING = " + "The source string is not specified to delete the sub-string");
        }
        if(tempStringBuffer != null)
            tempStringBuffer = null;
    }
    /**
     * Insert a string error handling
     */
    public void insertErrorHandling(String insert_str)
    {

    }
    }
